package core;

import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentLinkedQueue;

import utils.TwitterParseUtil;
import elements.Tuple;

public class MapWorker extends Thread {

	private String name;
	private String block;
	private ConcurrentLinkedQueue<Tuple> maps;
	
	public MapWorker(String name, String block, 
						ConcurrentLinkedQueue<Tuple> maps) {
		this.name = name;
		this.block = TwitterParseUtil.preformatBlock(block);
		this.maps = maps;
	}
	
	public void run() {
		WordCounter.aliveWorkers++;
		StringTokenizer tokenizer = new StringTokenizer(block);
		while (tokenizer.hasMoreTokens()) {
			String word = tokenizer.nextToken();
			word = TwitterParseUtil.preformatToken(word);

			if (TwitterParseUtil.isValidWord(word) && 
						!TwitterParseUtil.isTrivialWord(word))
				maps.add(new Tuple(word, 1));
		}
		WordCounter.aliveWorkers--;
	}
}
